package cn.schoolwow.workflow.module.task.action.flow.complete.transaction;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.domain.task.CompleteTaskRequest;
import cn.schoolwow.workflow.entity.WorkFlowTask;
import cn.schoolwow.workflow.module.common.flow.AddWorkFlowHistoryFlow;

public class AddCompleteTaskHistoryFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        DAO dao = flowContext.checkInstanceData(DAO.class);
        CompleteTaskRequest completeTaskRequest = flowContext.checkInstanceData(CompleteTaskRequest.class);

        dao.addRecordString("根据任务id查询工作流任务");
        WorkFlowTask workFlowTask = dao.query(WorkFlowTask.class)
                .addQuery("id", completeTaskRequest.taskId)
                .execute()
                .getOne();

        switch (completeTaskRequest.taskType){
            case COMPLETE:{
                flowContext.putTemporaryData("message", "审核通过任务["+workFlowTask.getTaskName()+"]");
            };break;
            case REJECT:{
                flowContext.putTemporaryData("message", "审核拒绝任务["+workFlowTask.getTaskName()+"],拒绝原因["+(null==completeTaskRequest.rejectReason?"无":completeTaskRequest.rejectReason)+"]");
            };break;
        }
        flowContext.startFlow(new AddWorkFlowHistoryFlow())
                .putCurrentCompositeFlowData("assigner", workFlowTask.getAssigner())
                .execute();
    }

    @Override
    public String name() {
        return "完成流程任务-添加审批历史记录";
    }

}
